//index.js
//获取应用实例
const app = getApp()

// var QQMapWX = require('../../../utils/qqmap-wx-jssdk');
// var qqmapsdk = new QQMapWX({
// 	key: 'RYOBZ-75B3N-WXFFQ-SS3LC-MWJUH-A4BY6' // 必填
// });
Page({
	data: {
		isData: false,
		is_advert: '',
		b_id: '',
		scale: '15',
		Height: '0',
		controls: '40',
		latitude: '',
		longitude: '',
		markers: [],
		subway: {
			'1号线': '#be3930',
			'2号线': '#1b6094',
			'4号线': '#1E8899',
			'5号线': '#A51C83',
			'6号线': '#CD9207',
			'7号线': '#FAC76E',
			'8号线': '#17996B',
			'9号线': '#8EC21F',
			'10号线': '#289BC0',
			'13号线': '#F3E20F',
			'14号线': '#D3A59F',
			'15号线': '#69357C',
			'16号线': '#729E2C',
			'房山线': '#E65E22',
			'昌平线': '#D47DAE',
			'亦庄线': '#E40074',
			'燕房线': '#DD5D22',
			'西郊线': '#E50618',
			'S1线': '#AE5821',
			'机场线': '#A49ABD'
		},
    url: app.globalData.BaseImgUrl,
    baseImgUrl: app.globalData.baseImgUrl,
		baseURL: 'https://lxcx-wxd.oss-cn-beijing.aliyuncs.com/',
		showMap: false
	},
	onReady: function (e) {
		// 使用 wx.createMapContext 获取 map 上下文 
		this.mapCtx = wx.createMapContext('map')
	},
	onLoad: function () {
		let that = this;
		// console.log('kaiiii');
		wx.getSystemInfo({
			success: function (res) {
				console.log(res);
				//设置map高度，根据当前设备宽高满屏显示
				that.setData({
					Height: res.windowHeight,
					showMap: true,
					controls: [{
							id: 1,
							iconPath: app.globalData.baseImgUrl+'/image/logo.jpg', //  大头针图片
							position: {
								left: res.windowWidth / 2 - 11,
								top: res.windowHeight / 2 - 70,
								width: 26,
								height: 45
							},
							clickable: true
						},
						{
							id: 2,
							iconPath: app.globalData.baseImgUrl+'/image/signed.png', // 左下角定位图片
							position: {
								left: 20,
								top: res.windowHeight - 100,
								width: 40,
								height: 40
							},
							clickable: true
						},
					]

				});
			}
		});

		wx.getLocation({
			type: 'gcj02', //返回可以用于wx.openLocation的经纬度
			success: (res) => {

				that.setData({
					longitude: res.longitude,
					latitude: res.latitude
				});
				that.getData(res.longitude, res.latitude);

				// that.setData({
				//     longitude: 116.337528,
				//     latitude: 39.991606
				// });
				// that.getData(116.337528, 39.991606);

			}
		});

		this.setData({
			is_advert: app.globalData.is_advert,
			b_id: app.globalData.b_id,
		});

	},
	serviceTel: function () {
		wx.makePhoneCall({
			phoneNumber: '01082658390',
		});
	},
	//请求设备信息
	getData: function (lon, lat) {
		var that = this,
			data = {};
		data.longitude = lon;
		data.latitude = lat;
		data.tsp = Math.floor(new Date().getTime() / 1000);

		data.sign = app.getSign(data);


		wx.request({
			url: app.globalData.Baseurl + 'coordinate/coordinateList',
			method: 'GET',
			data: data,
			success: function (res) {
				if (res.data.status) {
					var sdata = res.data.data;
					for (var j in sdata) {
						var arr = [],
							item = {};

						sdata[j].subway_line = sdata[j].subway_line.split(",");

						for (var i in sdata[j].subway_line) {
							item.text = sdata[j].subway_line[i];
							item.color = that.data.subway[sdata[j].subway_line[i]];

							arr.push(item);
						}

						sdata[j].subway_line = arr;
					}

					res.data.data = sdata;

					that.setData({
						isData: true,
						markers: that.getSchoolMarkers(res.data.data),
						scale: 18,
						longitude: lon,
						latitude: lat
					});
				} else {
					that.setData({
						isData: false
					});
				}


			}
		});

	},
	controltap: function (e) {
		console.log(e);
		this.moveToLocation()
	},
	getSchoolMarkers(list) {
		var market = [];

		if (!list) return market;

		for (let item of list) {

			let marker1 = this.createMarker(item);

			market.push(marker1)
		}
		return market;
	},
	moveToLocation: function () {
		this.mapCtx.moveToLocation();
	},
	createMarker(point) {
		var t = this;
		let latitude = point.latitude;
		let longitude = point.longitude;
		let marker = {
			iconPath: app.globalData.baseImgUrl+"/image/logo.jpg",
			id: point.id || 0,
			name: point.name || '',
			title: point.title || '', //点击显示
			latitude: latitude,
			longitude: longitude,
			distance: point.distance,
			subway_line: point.subway_line,
			labels: point.label.split(","),
			img: point.img,
			label: {
				x: -24,
				y: -38,
				content: point.name
			},
			// width: 30,
			// height: 30
		};
		return marker;
	},
	posTap: function (e) {
		console.log(e);
	},
	distance: function (la1, lo1, la2, lo2) {
		var La1 = la1 * Math.PI / 180.0;

		var La2 = la2 * Math.PI / 180.0;

		var La3 = La1 - La2;

		var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;

		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));

		s = s * 6378137; //地球半径

		// s = Math.round(s * 100) / 100;
		s = Math.round(s);

		return s

		// console.log("计算结果",s)
	},
	/**
	 * 纬度,经度
	 * f出发点坐标 
	 * t到达坐标
	 */
	navigatTo: function (e) {
		var _this = this,
			title = '',
			lat = '',
			lon = '',
			t = '',
			f = _this.data.latitude + ',' + _this.data.longitude;
		// console.log(e);return false;

		for (var m in _this.data.markers) {
			if (e.currentTarget.dataset.id == _this.data.markers[m].id) {
				t = _this.data.markers[m].latitude + ',' + _this.data.markers[m].longitude;
				lat = _this.data.markers[m].latitude;
				lon = _this.data.markers[m].longitude;
				title = _this.data.markers[m].name;
			}
		}

		wx.getLocation({ //获取当前经纬度
			type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
			success: function (res) {
				wx.openLocation({ //​使用微信内置地图查看位置。
					latitude: lat, //要去的纬度-地址
					longitude: lon, //要去的经度-地址
					name: title,
					address: ''
				});
			}
		});

	},
	// 视野发生变化时触发
	regionchange: function (e) {
		// console.log(e);
		let that = this;
		// console.log('视野发生变化时触发===', that)
		that.mpCtx = wx.createMapContext("map");
		that.mpCtx.getScale({ //获取当前地图的缩放级别
			success: (res) => {
				// console.log('地图缩放级别', res.scale)
				that.mpCtx.getCenterLocation({ //获取当前地图中心的经纬度。返回的是 gcj02 坐标系
					success: (restion) => {
						console.log(restion)
						that.setData({
							markers: [{
								id: 1,
								width: 25,
								height: 25,
								iconPath: '../../image/signing.png',
								longitude: restion.longitude,
								latitude: restion.latitude,
								title: restion.address
							}]
						}, () => {
							qqmapsdk.reverseGeocoder({ //逆地址解析
								location: {
									latitude: restion.latitude,
									longitude: restion.longitude
								},
								success: (res1) => {
									let res = res1.result
									console.log('逆地址解析res===', res1.result)
									that.setData({
										chooseLat: res1.result.location.lat,
										chooseLng: res1.result.location.lng,
										chooseAddress: res1.result.address,
										chooseRecommend: res1.result.formatted_addresses.recommend
									})
								}
							})
						})
					}
				})
			},
			fail: function () {
				console.log('获取当前地图的缩放级别失败===')
			}
		})

	}

})